package game;

import utils.RandomUtils;

import java.awt.*;
import java.awt.image.BufferedImage;

public class Pterosaur extends Sprite {


    BufferedImage pterosaur2;
    {
            pterosaur2 =  ImageManager.loadImage("src/Images/"+ GameData.Skin +"/pterosaur2.png");
    }

    public void init() {
        super.x = 600;
        super.y = 5+RandomUtils.randomInt(-5,20);
        super.image = ImageManager.loadImage("src/Images/"+ GameData.Skin +"/pterosaur1.png");
        super.width = super.image.getWidth();
        super.height = super.image.getHeight();
        super.speed = 4;
    }
    @Override
    public void move() {
        super.x = super.x - super.speed;
        if(x<-50){
            add_avoid_pterosaur();
            this.destroy();
        }
    }
    boolean flag = true;
    int cnt = 0;

    @Override
    public void draw(Graphics g) {
        offscreenImage = new BufferedImage(600,200,BufferedImage.TYPE_4BYTE_ABGR);
        offscreenGraphics = offscreenImage.createGraphics();
        if(cnt<40){
            if(flag){
                super.draw(super.offscreenGraphics);
            }else {
                offscreenGraphics.drawImage(pterosaur2,x,y,pterosaur2.getWidth(),pterosaur2.getHeight(),null);
            }
            cnt++;
        }else {
            flag = !flag;
            cnt =0;
        }
        g.drawImage(offscreenImage,0,0,null);
        offscreenImage.flush();
    }
    public Rectangle getBounds() {
        return new Rectangle(x+10,y+5,width-28,height-30);
    }
    @Override
    public void destroy() {
        init();
    }
    private void add_avoid_pterosaur(){
        GameData.avoid_pterosaur++;
    }

}
